const BasicModel = require('./BasicModel');
module.exports = class extends BasicModel {
  async addnoticeAction(params) {
    const id = think.uuid('v4');
    params.data.id = id;
    const textdata = {
      richtext: params.data.richtext,
      relation_id: id,
      status: 0
    };
    await this.model(params.data.type).add(think.omit(params.data, 'richtext'));
    const richtextID = think.uuid('v4');
    textdata.id = richtextID;
    await this.model('richtext').add(textdata);
    const obj = {id: id};
    return obj;
  }
  async editnoticeAction(params) {
    const textdata = {
      richtext: params.data.richtext
    };
    const data = await this.model(params.data.type).where({id: params.data.id}).update(think.omit(params.data, 'richtext,type'));
    await this.model('richtext').where({relation_id: params.data.id}).update(textdata);
    return data;
  }
  async getAction(params) {
    const data = await this.model(params.data.type).where(think.omit(params.data, 'type')).find();
    let textdata = await this.model('richtext').where({relation_id: params.data.id}).find();
    if (textdata.richtext) {
      textdata = textdata.richtext.toString('utf-8');
      data.richtext = textdata;
      return data;
    } else {
      data.richtext = '';
      return data;
    }
  }
  async delAction(params) {
    await this.model(params.data.type).where(think.omit(params.data, 'type')).delete();
    await this.model('user_notice').where({notice_id: params.data.id}).delete();
    const data = await this.model('richtext').where({relation_id: params.data.id}).delete();
    return data;
  }
  async saveAction(params) {
    const insertdata = {
      status: 0
    };
    const data = await this.model(params.data.type).where(think.omit(params.data, 'type')).update(insertdata);
    return data;
  }
  async releaseAction(params) {
    const insertdata = {
      status: '1'
    };
    const data = await this.model(params.data.type).where(think.omit(params.data, 'type')).update(insertdata);
    return data;
  }
  async listpageAction(params) {
    const DATA = {};
    const NEWdDATA = {};
    for (const i in params.data) {
      DATA[i] = params.data[i];
    }
    for (const i in DATA) {
      NEWdDATA[i] = DATA[i];
    }
    const data = await this.model(params.data.type).where(think.omit(NEWdDATA, 'page,pageSize,type')).page(DATA.page || 1, DATA.pageSize || 20).order('create_time DESC').countSelect();
    const databack = {};
    databack.pageSize = data.pageSize;
    databack.current = data.currentPage;
    databack.pages = data.totalPages;
    databack.total = data.count;
    databack.data = data.data;
    return databack;
  }
};
